package com.ruoyi.web.controller.honor.studenthonor;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.honor.studenthonor.StudentAchRequestVO;
import com.ruoyi.system.domain.honor.studenthonor.StudentAchievements;
import com.ruoyi.system.domain.honor.studenthonor.StudentAchievementsVo;
import com.ruoyi.system.service.honor.IStudentAchievementsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 在校生代性成果Controller
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/honor/studentAch")
public class StudentAchievementsController extends BaseController
{
    @Autowired
    private IStudentAchievementsService studentAchievementsService;

    /**
     * 查询在校生代性成果列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StudentAchievements studentAchievements)
    {
        startPage();
        List<StudentAchievements> list = studentAchievementsService.selectStudentAchievementsList(studentAchievements);
        return getDataTable(list);
    }

    /**
     * 导出在校生代性成果列表
     */
    @Log(title = "在校生代性成果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentAchievements studentAchievements,
        @RequestParam(required = false) String exportFields)
    {
        List<StudentAchievements> list = studentAchievementsService.selectStudentAchievementsList(studentAchievements);
        List<StudentAchievementsVo> listVo = new ArrayList<>();
        for (StudentAchievements ra : list) {
            StudentAchievementsVo vo = new StudentAchievementsVo();
            vo.setStudentName(ra.getStudentName());
            vo.setAchievementCategory(ra.getAchievementCategory());
            vo.setObtainmentTime(ra.getObtainmentTime());
            vo.setAchievementBrief(ra.getAchievementBrief());
            vo.setStudentParticipation(ra.getStudentParticipation());
            vo.setTeacherName(ra.getTeacherName());
            vo.setEnrollmentTime(ra.getEnrollmentTime());
            vo.setDegreeType(ra.getDegreeType());
            vo.setLearningMethod(ra.getLearningMethod());
            listVo.add(vo);
        }
        ExcelUtil<StudentAchievementsVo> util = new ExcelUtil<StudentAchievementsVo>(StudentAchievementsVo.class);
        // 如果传递了导出字段，则进行处理
        if (StringUtils.isNotBlank(exportFields)) {
            // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
            String[] fields = exportFields.split(",");
            util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
        }
        util.exportExcel(response, listVo, "在校生代性成果数据");
    }

    /**
     * 获取在校生代性成果详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentAchievementsService.selectStudentAchievementsById(id));
    }

    /**
     * 新增在校生代性成果
     */
    @Log(title = "在校生代性成果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentAchRequestVO studentAchievements)
    {
        return toAjax(studentAchievementsService.insertStudentAchievements(studentAchievements));
    }

    /**
     * 修改在校生代性成果
     */
    @Log(title = "在校生代性成果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentAchievements studentAchievements)
    {
        return toAjax(studentAchievementsService.updateStudentAchievements(studentAchievements));
    }

    /**
     * 删除在校生代性成果
     */
    @Log(title = "在校生代性成果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentAchievementsService.deleteStudentAchievementsByIds(ids));
    }
}
